<%@page import="java.util.ArrayList"%>
<%@page import="com.xhd.vo.Order"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.xhd.vo.Fruits"%>
<%@page import="java.util.List"%>
<%@page import="com.xhd.Dao.SalerDaoImpl"%>
<%@page import="com.xhd.Dao.SalerDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<style type="text/css">
	* {
		font-size: 11pt;
	}
	div {
		border: solid 2px rgb(78,78,78);
		width: 75px;
		height: 75px;
		text-align: center;
	}
	li {
		margin: 10px;
	}
	#sold{
		background-color: #EECBAD;
	}
	#freezed{
		background-color: #33ffff;
	}
	#ready{
		background-color: #7CCD7C;	
	}
</style>
</head>
 <body style="background: rgb(254,238,189);">
	<c:if test="${ not empty sessionScope.user}">
	 <form action="SalerServlet?method=serchFruitinSaler" method="post">
 	<input type="text"  name="serchfruitinsaler">	<input type="submit"  name="serchinsaler" value="查找商品">
 	</form>
	<% 
	List<Fruits> serchlistinsaler = new ArrayList<Fruits>();
	serchlistinsaler=(List<Fruits>)request.getAttribute("serchlistinsaler");
	if(serchlistinsaler==null){

	SalerDao sd = new SalerDaoImpl();
	List<Fruits> fruitlist1=sd.haveFruitOrNot("已售出");
	Iterator<Fruits> fIter1=fruitlist1.iterator();
	
	List<Fruits> fruitlist2=sd.haveFruitOrNot("被冻结");
	Iterator<Fruits> fIter2=fruitlist2.iterator();
	
	List<Fruits> fruitlist3=sd.haveFruitOrNot("待售");
	Iterator<Fruits> fIter3=fruitlist3.iterator();
	int i=0;
	String formname="";
	if(!fIter1.hasNext()&&!fIter2.hasNext()&&!fIter3.hasNext()){
	%>
		没有商品记录！
	<% 
	}
	else{
	Fruits fruit=null;
	%>
	<ul id="freezed">
		<%	while(fIter2.hasNext()){
			fruit=fIter2.next();
			List<Order> orderlist=sd.showOrderByFruit(fruit.getFruit_name());
			Iterator<Order> oIter=orderlist.iterator();
			i++;
			formname = "form"+i;
		%>
		<li><%=fruit.getFruit_name() %>&nbsp;&nbsp;水果状态:<%=fruit.getFruit_state() %>

		</li>
					<form name='<%=formname%>' action='SalerCheckFruit.jsp' method='post'>     
					<input type='hidden' name='name' value='<%=fruit.getFruit_name()%>'/>  	  
					<input type='hidden' name='price' value='<%=fruit.getFruit_price()%>'/> 
					<input type='hidden' name='describe' value='<%=fruit.getFruit_describe()%>'/> 
					<input type='hidden' name='picture' value='<%=fruit.getFruit_picture()%>'/> 
					<input type='hidden' name='stock' value='<%=fruit.getFruit_stock()%>'/>
					<a href='javascript:document.<%=formname%>.submit();'>商品详情</a>  
					</form>
		<%if(oIter.hasNext()){
			Order order=null;
		%>
		<details open><summary>相关订单</summary>
		<table border="1" width="100%" cellspacing="0" background="black">
			<tr bgcolor="rgb(78,78,78)" bordercolor="rgb(78,78,78)" align="center" style="color: white;">
				<td width="100px">买家</td>
				<td width="200px">手机号码</td>
				<td width="200px">配送地址</td>
				<td width="200px">购买数量(kg)</td>
				<td width="200px">订单状态</td>
				<td width="200px">订单处理</td>
			</tr>
			<%
				while(oIter.hasNext()){
				order=oIter.next();
			%>
			<tr bordercolor="rgb(78,78,78)" align="center">
				<td><%=order.getOrder_buyer() %></td>
				<td><%=order.getOrder_phone() %></td>
				<td><%=order.getOrder_address()%></td>
				<td><%=order.getOrder_sum()%></td>
				<%if ((order.getOrder_state().equals("待处理")||order.getOrder_state().equals("意向订单")) && Integer.parseInt(order.getOrder_sum())>Integer.parseInt(fruit.getFruit_stock()) ){
					sd.changeOrderState("out", fruit.getFruit_name(), order.getOrder_buyer());
				%>
					<td>需求数大于存货，订单失效</td>
					<td>订单已处理</td>
				<% 
				}else{	
				%>
				<td><%=order.getOrder_state() %></td>
				<td><%if (order.getOrder_state().equals("待处理")){%>
    				<a href="TradingIntention.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">意向交易</a>
     				<% } 
					else if (order.getOrder_state().equals("意向订单")){
    				 %>
    				<a href="OrderSuccess.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>&sum=<%=order.getOrder_sum() %>&stock=<%=fruit.getFruit_stock()%>">交易成功</a><br>
    				<a href="OrderFail.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">取消交易</a>
    				<%	
    				}
					else if (order.getOrder_state().equals("非意向订单")){
					%>
					有关于该商品的订单未处理！
					<% 
					}
	  				else{
					%>
					订单已处理
					<%	  
	  				}
   					 %>
   				</td>
   				<%} %>
			</tr>

		<%
			}
		}
		%>
		</table>
		</details>
		<% }
		%>
		</ul>
		<ul id="ready">
		<%	while(fIter3.hasNext()){
			fruit=fIter3.next();
			List<Order> orderlist=sd.showOrderByFruit(fruit.getFruit_name());
			Iterator<Order> oIter=orderlist.iterator();
			i++;
			formname = "form"+i;
		%>
		<li><%=fruit.getFruit_name() %>&nbsp;&nbsp;水果状态:<%=fruit.getFruit_state() %>

		</li>
					<form name='<%=formname%>' action='SalerCheckFruit.jsp' method='post'>     
					<input type='hidden' name='name' value='<%=fruit.getFruit_name()%>'/>  	  
					<input type='hidden' name='price' value='<%=fruit.getFruit_price()%>'/> 
					<input type='hidden' name='describe' value='<%=fruit.getFruit_describe()%>'/> 
					<input type='hidden' name='picture' value='<%=fruit.getFruit_picture()%>'/>
					<input type='hidden' name='stock' value='<%=fruit.getFruit_stock()%>'/> 
					<a href='javascript:document.<%=formname%>.submit();'>商品详情</a>  
					</form>
		<%if(oIter.hasNext()){
			Order order=null;
		%>
		<details open><summary>相关订单</summary>
		<table border="1" width="100%" cellspacing="0" background="black">
			<tr bgcolor="rgb(78,78,78)" bordercolor="rgb(78,78,78)" align="center" style="color: white;">
				<td width="100px">买家</td>
				<td width="200px">手机号码</td>
				<td width="200px">配送地址</td>
				<td width="200px">购买数量(kg)</td>
				<td width="200px">订单状态</td>
				<td width="200px">订单处理</td>
			</tr>
			<%
				while(oIter.hasNext()){
				order=oIter.next();
			%>
			<tr bordercolor="rgb(78,78,78)" align="center">
				<td><%=order.getOrder_buyer() %></td>
				<td><%=order.getOrder_phone() %></td>
				<td><%=order.getOrder_address()%></td>
				<td><%=order.getOrder_sum()%></td>
				<%if ((order.getOrder_state().equals("待处理")||order.getOrder_state().equals("意向订单")) && Integer.parseInt(order.getOrder_sum())>Integer.parseInt(fruit.getFruit_stock()) ){
					sd.changeOrderState("out", fruit.getFruit_name(), order.getOrder_buyer());
				%>
					<td>需求数大于存货，订单失效</td>
					<td>订单已处理</td>
				<% 
				}else{	
				%>
				<td><%=order.getOrder_state() %></td>
				<td><%if (order.getOrder_state().equals("待处理")){%>
    				<a href="TradingIntention.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">意向交易</a>
     				<% } 
					else if (order.getOrder_state().equals("意向订单")){
    				 %>
    				<a href="OrderSuccess.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">交易成功</a><br>
    				<a href="OrderFail.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">取消交易</a>
    				<%	
    				}
					else if (order.getOrder_state().equals("非意向订单")){
					%>
					有关于该商品的订单未处理！
					<% 
					}
	  				else{
					%>
					订单已处理
					<%	  
	  				}
   					 %>
   				</td>
   				<%} %>
			</tr>

		<%
			}
		}
		%>
		</table>
		</details>
		<% }
		%>
		</ul>
		<ul id="sold">
		<%	while(fIter1.hasNext()){
			fruit=fIter1.next();
			List<Order> orderlist=sd.showOrderByFruit(fruit.getFruit_name());
			Iterator<Order> oIter=orderlist.iterator();
		%>
		<li><%=fruit.getFruit_name() %>&nbsp;&nbsp;水果状态:<%=fruit.getFruit_state() %>

		</li>
		<details><summary>水果信息</summary>
		<table border="1" width="100%" cellspacing="0" background="black">
		<tr bgcolor="rgb(78,78,78)" bordercolor="rgb(78,78,78)" align="center" style="color: white;">
		<th>水果名</th>
		<th>水果图片</th>
		<th>水果描述</th>
		<th>水果价格</th>		
		</tr>
		<tr bordercolor="rgb(78,78,78)" align="center">
		<td><%=fruit.getFruit_name() %></td>
			<td>
	<%
	int left = 0;
	int right = fruit.getFruit_picture().indexOf("@");
	while(right!=-1) {
		String imagepath = fruit.getFruit_picture().substring(left,right);
		//System.out.println("1:"+imagepath);
		%>
		<img style="height:100px; weight:100px;" src="./upload/<%=imagepath %>">
		<%
		left=right+1;
		right=fruit.getFruit_picture().indexOf("@",right+1);
		}
		String imagepath = fruit.getFruit_picture().substring(left,fruit.getFruit_picture().length());
		//System.out.println("2:"+imagepath);
		%>
		<img style="height:100px; weight:100px;" src="./upload/<%=imagepath %>"></td>
		<td><%=fruit.getFruit_describe() %></td>
		<td><%=fruit.getFruit_price() %></td>
		</tr>
		</table>
		</details>
		<%if(oIter.hasNext()){
			Order order=null;
		%>
		<details ><summary>相关订单</summary>
		<table border="1" width="100%" cellspacing="0" background="black">
			<tr bgcolor="rgb(78,78,78)" bordercolor="rgb(78,78,78)" align="center" style="color: white;">
				<td width="100px">买家</td>
				<td width="200px">手机号码</td>
				<td width="200px">配送地址</td>
				<td width="200px">购买数量(kg)</td>
				<td width="200px">订单状态</td>
				<td width="200px">订单处理</td>
			</tr>
			<%
				while(oIter.hasNext()){
				order=oIter.next();
			%>
			<tr bordercolor="rgb(78,78,78)" align="center">
				<td><%=order.getOrder_buyer() %></td>
				<td><%=order.getOrder_phone() %></td>
				<td><%=order.getOrder_address()%></td>
				<td><%=order.getOrder_sum()%></td>
				<%if ((order.getOrder_state().equals("待处理")||order.getOrder_state().equals("意向订单")) && Integer.parseInt(order.getOrder_sum())>Integer.parseInt(fruit.getFruit_stock()) ){
					sd.changeOrderState("out", fruit.getFruit_name(), order.getOrder_buyer());
				%>
					<td>需求数大于存货，订单失效</td>
					<td>订单已处理</td>
				<% 
				}else{	
				%>
				<td><%=order.getOrder_state() %></td>
				<td><%if (order.getOrder_state().equals("待处理")){%>
    				<a href="TradingIntention.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">意向交易</a>
     				<% } 
					else if (order.getOrder_state().equals("意向订单")){
    				 %>
    				<a href="OrderSuccess.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">交易成功</a><br>
    				<a href="OrderFail.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">取消交易</a>
    				<%	
    				}
					else if (order.getOrder_state().equals("非意向订单")){
					%>
					有关于该商品的订单未处理！
					<% 
					}
	  				else{
					%>
					订单已处理
					<%	  
	  				}
   					 %>
   				</td>
   				<%} %>
			</tr>

		<%
			}
		}
		%>
		</table>
		</details>
		<% }
		%>
	</ul>
		<%
	}
	}else{
		Iterator<Fruits> sfIter=serchlistinsaler.iterator();
		if (!sfIter.hasNext()){
		%>	
			抱歉，找不到商品！<br>		
			<a href="HistoryMessage.jsp">点击此处</a>返回
		<%	
		}
		else{
			int i=1000;
			String formname="";
			Fruits serchfruit=null;
			String state="";
			while(sfIter.hasNext()){
				serchfruit=sfIter.next();
				if(serchfruit.getFruit_state().equals("待售")){
					state="ready";
				}
				else if(serchfruit.getFruit_state().equals("被冻结")){
					state="freezed";
				}
				else{
					state="sold";
				}
			%>
		<ul id=<%=state %>>
		<%	
			SalerDao sd=new SalerDaoImpl();
			List<Order> orderlist=sd.showOrderByFruit(serchfruit.getFruit_name());
			Iterator<Order> oIter=orderlist.iterator();
			i++;
			formname = "form"+i;
		%>
		<li><%=serchfruit.getFruit_name() %>&nbsp;&nbsp;水果状态:<%=serchfruit.getFruit_state() %>

		</li>
					<form name='<%=formname%>' action='SalerCheckFruit.jsp' method='post'>     
					<input type='hidden' name='name' value='<%=serchfruit.getFruit_name()%>'/>  	  
					<input type='hidden' name='price' value='<%=serchfruit.getFruit_price()%>'/> 
					<input type='hidden' name='describe' value='<%=serchfruit.getFruit_describe()%>'/> 
					<input type='hidden' name='picture' value='<%=serchfruit.getFruit_picture()%>'/> 
					<input type='hidden' name='stock' value='<%=serchfruit.getFruit_stock()%>'/>
					<a href='javascript:document.<%=formname%>.submit();'>商品详情</a>  
					</form>
		<%if(oIter.hasNext()){
			Order order=null;
		%>
		<details ><summary>相关订单</summary>
		<table border="1" width="100%" cellspacing="0" background="black">
			<tr bgcolor="rgb(78,78,78)" bordercolor="rgb(78,78,78)" align="center" style="color: white;">
				<td width="100px">买家</td>
				<td width="200px">手机号码</td>
				<td width="200px">配送地址</td>
				<td width="200px">购买数量(kg)</td>
				<td width="200px">订单状态</td>
				<td width="200px">订单处理</td>
			</tr>
			<%
				while(oIter.hasNext()){
				order=oIter.next();
			%>
			<tr bordercolor="rgb(78,78,78)" align="center">
				<td><%=order.getOrder_buyer() %></td>
				<td><%=order.getOrder_phone() %></td>
				<td><%=order.getOrder_address()%></td>
				<td><%=order.getOrder_sum()%></td>
				<%if ((order.getOrder_state().equals("待处理")||order.getOrder_state().equals("意向订单")) && Integer.parseInt(order.getOrder_sum())>Integer.parseInt(serchfruit.getFruit_stock()) ){
					sd.changeOrderState("out", serchfruit.getFruit_name(), order.getOrder_buyer());
				%>
					<td>需求数大于存货，订单失效</td>
					<td>订单已处理</td>
				<% 
				}else{	
				%>
				<td><%=order.getOrder_state() %></td>
				<td><%if (order.getOrder_state().equals("待处理")){%>
    				<a href="TradingIntention.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">意向交易</a>
     				<% } 
					else if (order.getOrder_state().equals("意向订单")){
    				 %>
    				<a href="OrderSuccess.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">交易成功</a><br>
    				<a href="OrderFail.jsp?fruit=<%=order.getOrder_fruit() %>&buyer=<%=order.getOrder_buyer() %>">取消交易</a>
    				<%	
    				}
					else if (order.getOrder_state().equals("非意向订单")){
					%>
					有关于该商品的订单未处理！
					<% 
					}
	  				else{
					%>
					订单已处理
					<%	  
	  				}
   					 %>
   				</td>
   				<%} %>
			</tr>

		<%
			}
		}
		%>
		</table>
		</details>
	</ul>
	<a href="HistoryMessage.jsp">查看所有商品</a>
			<%
			}
		}
	}
	%>
	</c:if>
<c:if test="${  empty sessionScope.user}">
<h1>你还未登录！</h1>
<a href="SalerLogin.jsp">点击登录</a>
</c:if>

</body>
</html>